/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.sql.*;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author sun
 */
public class SqlDataSourse implements server.DataSource {

    private String classname = "sun.jdbc.odbc.JdbcOdbcDriver";
    private static final String splname = "jdbc:odbc:chat";
    Connection con;
    Statement st;
    ResultSet rs;

    @Override
    public boolean verifyUser(String user, String pass) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "select * from user where uname='" + user + "'and pass ='" + pass + "'";
            rs = st.executeQuery(user);
            if (rs.next()) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean addUser(String user, String pass) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "insert into user values(" + user + "," + pass + ")";
            return st.execute(sql);
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean removeUser(String user, String pass) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "delete from user where uname='" + user + "'and pass ='" + pass + "'";
            return st.execute(sql);
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean addInfo(String user, String field, String info) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "insert into info values(" + user + "," + field + "," + info + ")";
            return st.execute(sql);
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public boolean removeInfo(String user, String field, String info) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "delete from info where uname ='" + user + "' and field ='" + field + "' and inf ='" + info + "'";
            return st.execute(sql);
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public String[] getAllUserInfo(String user) {
        String[] info = {"field", "data"};
        return info;
    }

    @Override
    public String[] getUserList() {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "selcet uname form user";
            rs = st.executeQuery(sql);
            LinkedList<String> list = new LinkedList();
            while (rs.next()) {
                list.add(rs.getString(1));
            }
            return (String[]) list.toArray();
        } catch (Exception ex) {
            return null;
        }
    }

    public boolean hasUser(String u) {
        try {
            Class.forName(classname);
            con = DriverManager.getConnection(splname);
            st = con.createStatement();
            String sql = "selcet * form user where uname='"+u+"'";
            rs = st.executeQuery(sql);
            if(rs.next()){
                return true;
            }else{
                return false;
            }

        } catch (Exception ex) {
        }
        return true;
    }
}
